package com.jichaoyun.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jichaoyun.model.app.AppCollection;
import com.jichaoyun.model.app.vos.MyCollectionVo;
import com.jichaoyun.model.entity.StationInfo;

import com.jichaoyun.common.model.Result;
import com.jichaoyun.model.entity.RealdataCache;

import com.jichaoyun.sys.mapper.AppCollectionMapper;
import com.jichaoyun.sys.mapper.RealdataCacheMapper;
import com.jichaoyun.sys.mapper.StationInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhen
 */
@RestController
@Api(tags={"app首页相关的接口"})
@RequestMapping("/sys/appHome")
public class AppHomeController {

    @Autowired
    private AppCollectionMapper appCollectionMapper;

    @Autowired
    private StationInfoMapper stationInfoMapper;

    @Autowired
    private RealdataCacheMapper realdataCacheMapper;


    @ApiOperation("app首页加载我的关注的信息")
    @GetMapping("/getMyCollections")
    public Result<List<MyCollectionVo>> getMyCollections(Integer uid){
        List<MyCollectionVo> res = new ArrayList<>();

        //查找其所有关注的用户
        LambdaQueryWrapper<AppCollection> wp = new LambdaQueryWrapper<>();
        wp.eq(AppCollection::getUserId,uid);
        List<AppCollection> appCollections = appCollectionMapper.selectList(wp);
        if(appCollections.size()==0){
            return Result.success(null);
        }
        //得到所有关注的用户的id
        ArrayList<Integer>  myCollectionUsers= new ArrayList<>();
        for (AppCollection appCollection : appCollections) {
            myCollectionUsers.add(appCollection.getCollectionUser());
        }

        for (Integer myCollectionUser : myCollectionUsers) {
            //查找这个用户所拥有蒸汽站点
            LambdaQueryWrapper<StationInfo> wp1 = new LambdaQueryWrapper<>();
            wp1.eq(StationInfo::getAppUserId,myCollectionUser);
            List<StationInfo> stationInfos = stationInfoMapper.selectList(wp1);
            if(stationInfos.size()==0){
                continue;
            }
            //将其站点的实时数据存入vo对象
            for (StationInfo stationInfo : stationInfos) {
                MyCollectionVo myCollectionVo = new MyCollectionVo();
                RealdataCache realdataCache = realdataCacheMapper.selectRecentOneByStation(stationInfo.getStationNo());
                myCollectionVo.setStationName(stationInfo.getStationName());
                myCollectionVo.setFlow(realdataCache.getFlow());
                myCollectionVo.setPressure(realdataCache.getPres());
                myCollectionVo.setTemp(realdataCache.getTemp());
                res.add(myCollectionVo);
            }

//            //查找这个用户所拥有的水的站点
//            List<StationInfo> water_stationInfos = stationInfoMapper.selectWaterStationsByUid(myCollectionUser);
//            if(water_stationInfos.size()==0){
//                continue;
//            }
//            //将其站点的实时数据存入vo对象
//            for (StationInfo stationInfo : water_stationInfos) {
//                MyCollectionVo myCollectionVo = new MyCollectionVo();
//                RealdataCache realdataCache = realdataCacheMapper.selectRecentWaterOneByStation(stationInfo.getStationNo());
//                myCollectionVo.setStationName(stationInfo.getStationName());
//                myCollectionVo.setFlow(realdataCache.getFlow());
//                myCollectionVo.setPressure(realdataCache.getPres());
//                myCollectionVo.setTemp(realdataCache.getTemp());
//                res.add(myCollectionVo);
//            }

        }
        return Result.success(res);
    }


}
